// index.js
Page({
    data: {
        cid: 1,
        movies: [],
        cityname: '未选择',
    },

    handleChangeNav(e) {
        let id = e.target.dataset.id
        this.setData({
            cid: id
        })

        wx.getStorage({
            key: id,
            success: (res) => {
                console.log('缓存找到了', res);
                this.setData({
                    movies: res.data
                })
            },
            fail: (err) => {
                console.warn('找不到了', err);
                this.loadData(id, 0).then(data => {
                    this.setData({
                        movies: data
                    })

                    wx.setStorage({
                        key: id,
                        data: data
                    })
                })
            }
        })

    },

    loadData(cid, offset) {
        return new Promise((resolve, reject) => {

            wx.showLoading({
                title: '加载中....',
            })


            wx.request({
                url: 'https://api.tedu.cn/index.php',
                method: 'GET',
                data: {
                    cid,
                    offset
                },
                success: (res) => {
                    console.log('首页列表加载结果', res);
                    resolve(res.data)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    wx.hideLoading()
                }
            })
        })


    },

    onLoad() {
        this.loadCurrentCity()
        this.loadData(1, 0).then(data => {
            this.setData({
                movies: data,
                cid: 1,
                cityname: ''
            })
        })
    },

    onShow() {
        let cityname = getApp().globalData.cityname
        console.log('onshow获取城市' + cityname);
        this.setData({
            cityname
        })
    },


    loadCurrentCity() {
        let QQMapWX = require('../../libs/qqmap-wx-jssdk.js')
        let qqmapskd = new QQMapWX({

            key: 'A7CBZ-FZ73U-PUPV7-BINEG-ICD57-KAB6J'
        })

        qqmapskd.reverseGeocoder({
            success: (res) => {
                let cityname = res.result.address_component.city
                console.log('您地址解析', cityname);
                this.setData({
                    cityname
                })

            },
            fail: (err) => {
                console.warn(err);
            }
        })


    },


    onReachBottom() {
        console.log('滚到低了');

        let cid = this.data.cid
        let offset = this.data.movies.length

        this.loadData(cid, offset).then(data => {
            this.setData({
                movies: this.data.movies.concat(data)
            })
        })
    },

    onPullDownRefresh() {
        console.log('下拉刷新');

        let cid = this.data.cid
        this.loadData(cid, 0).then(data => {
            this.setData({
                movies: data
            })
            wx.stopPullDownRefresh()

            wx.setStorage({
                key: cid + "",
                data: data
            })

        })
    }

})